<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Join (SQL)</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Join_(SQL)"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.math.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Join_SQL rootpage-Join_SQL skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Join (SQL)</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<p>A <b>join</b> clause in the Structured Query Language (<a href="SQL" title="SQL">SQL</a>) combines <a href="Column_(database)" title="Column (database)">columns</a> from one or more <a href="Table_(database)" title="Table (database)">tables</a> into a new table. The operation corresponds to a <a href="Join_(relational_algebra)" title="Join (relational algebra)">join operation in relational algebra</a>. Informally, a join stitches two tables and puts on the same row records with matching fields. There are several variants of <code>JOIN</code>: <code>INNER</code>, <code>LEFT OUTER</code>, <code>RIGHT OUTER</code>, <code>FULL OUTER</code>, <code>CROSS</code>, and others.
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Example_tables">Example tables</h2></div>
<p>To explain join types, the rest of this article uses the following tables:
</p>
<table class="wikitable" style="text-align:center; float:left; margin-right:5px">
<caption>Employee table
</caption>
<tbody><tr>
<th>LastName</th>
<th>DepartmentID
</th></tr>
<tr>
<td>Rafferty</td>
<td>31
</td></tr>
<tr>
<td>Jones</td>
<td>33
</td></tr>
<tr>
<td>Heisenberg</td>
<td>33
</td></tr>
<tr>
<td>Robinson</td>
<td>34
</td></tr>
<tr>
<td>Smith</td>
<td>34
</td></tr>
<tr>
<td>Williams</td>
<td><code style="color: white; background-color: gray; padding: 2px 4px; font-size: smaller;">NULL</code>
</td></tr></tbody></table>
<table class="wikitable" style="text-align:center; float:left; margin-left:5px">
<caption>Department table
</caption>
<tbody><tr>
<th>DepartmentID</th>
<th>DepartmentName
</th></tr>
<tr>
<td>31</td>
<td>Sales
</td></tr>
<tr>
<td>33</td>
<td>Engineering
</td></tr>
<tr>
<td>34</td>
<td>Clerical
</td></tr>
<tr>
<td>35</td>
<td>Marketing
</td></tr></tbody></table>
<div style="clear:both;" class=""></div>
<p><code>Department.DepartmentID</code> is the <a href="Primary_key" title="Primary key">primary key</a> of the <code>Department</code> table, whereas <code>Employee.DepartmentID</code> is a <a href="Foreign_key" title="Foreign key">foreign key</a>.
</p><p>Note that in <code>Employee</code>, "Williams" has not yet been assigned to a department. Also, no employees have been assigned to the "Marketing" department.
</p><p>These are the SQL statements to create the above tables:
</p>
<div class="mw-highlight mw-highlight-lang-sql mw-content-ltr mw-highlight-lines" dir="ltr"><pre><span class="k">CREATE</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="n">department</span><span class="p">(</span>
<span class="w"> </span><span class="n">DepartmentID</span><span class="w"> </span><span class="nb">INT</span><span class="w"> </span><span class="k">PRIMARY</span><span class="w"> </span><span class="k">KEY</span><span class="w"> </span><span class="k">NOT</span><span class="w"> </span><span class="k">NULL</span><span class="p">,</span>
<span class="w"> </span><span class="n">DepartmentName</span><span class="w"> </span><span class="nb">VARCHAR</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
<span class="p">);</span>
<span class="k">CREATE</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="n">employee</span><span class="w"> </span><span class="p">(</span>
<span class="w"> </span><span class="n">LastName</span><span class="w"> </span><span class="nb">VARCHAR</span><span class="p">(</span><span class="mi">20</span><span class="p">),</span>
<span class="w"> </span><span class="n">DepartmentID</span><span class="w"> </span><span class="nb">INT</span><span class="w"> </span><span class="k">REFERENCES</span><span class="w"> </span><span class="n">department</span><span class="p">(</span><span class="n">DepartmentID</span><span class="p">)</span>
<span class="p">);</span>
<span class="k">INSERT</span><span class="w"> </span><span class="k">INTO</span><span class="w"> </span><span class="n">department</span>
<span class="k">VALUES</span><span class="w"> </span><span class="p">(</span><span class="mi">31</span><span class="p">,</span><span class="w"> </span><span class="s1">'Sales'</span><span class="p">),</span>
<span class="w"> </span><span class="p">(</span><span class="mi">33</span><span class="p">,</span><span class="w"> </span><span class="s1">'Engineering'</span><span class="p">),</span>
<span class="w"> </span><span class="p">(</span><span class="mi">34</span><span class="p">,</span><span class="w"> </span><span class="s1">'Clerical'</span><span class="p">),</span>
<span class="w"> </span><span class="p">(</span><span class="mi">35</span><span class="p">,</span><span class="w"> </span><span class="s1">'Marketing'</span><span class="p">);</span>
<span class="k">INSERT</span><span class="w"> </span><span class="k">INTO</span><span class="w"> </span><span class="n">employee</span>
<span class="k">VALUES</span><span class="w"> </span><span class="p">(</span><span class="s1">'Rafferty'</span><span class="p">,</span><span class="w"> </span><span class="mi">31</span><span class="p">),</span>
<span class="w"> </span><span class="p">(</span><span class="s1">'Jones'</span><span class="p">,</span><span class="w"> </span><span class="mi">33</span><span class="p">),</span>
<span class="w"> </span><span class="p">(</span><span class="s1">'Heisenberg'</span><span class="p">,</span><span class="w"> </span><span class="mi">33</span><span class="p">),</span>
<span class="w"> </span><span class="p">(</span><span class="s1">'Robinson'</span><span class="p">,</span><span class="w"> </span><span class="mi">34</span><span class="p">),</span>
<span class="w"> </span><span class="p">(</span><span class="s1">'Smith'</span><span class="p">,</span><span class="w"> </span><span class="mi">34</span><span class="p">),</span>
<span class="w"> </span><span class="p">(</span><span class="s1">'Williams'</span><span class="p">,</span><span class="w"> </span><span class="k">NULL</span><span class="p">);</span>
</pre></div>
<div class="mw-heading mw-heading2"><h2 id="Cross_join">Cross join</h2></div>
<p><code>CROSS JOIN</code> returns the <a href="Cartesian_product" title="Cartesian product">Cartesian product</a> of rows from tables in the join. In other words, it will produce rows which combine each row from the first table with each row from the second table.<sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup>
</p>
<table class="wikitable" style="text-align:center">
<tbody><tr>
<th>Employee.LastName</th>
<th>Employee.DepartmentID</th>
<th>Department.DepartmentName</th>
<th>Department.DepartmentID
</th></tr>
<tr>
<td>Rafferty</td>
<td>31</td>
<td>Sales</td>
<td>31
</td></tr>
<tr>
<td>Jones</td>
<td>33</td>
<td>Sales</td>
<td>31
</td></tr>
<tr>
<td>Heisenberg</td>
<td>33</td>
<td>Sales</td>
<td>31
</td></tr>
<tr>
<td>Smith</td>
<td>34</td>
<td>Sales</td>
<td>31
</td></tr>
<tr>
<td>Robinson</td>
<td>34</td>
<td>Sales</td>
<td>31
</td></tr>
<tr>
<td>Williams</td>
<td><code style="color: white; background-color: gray; padding: 2px 4px; font-size: smaller;">NULL</code></td>
<td>Sales</td>
<td>31
</td></tr>
<tr>
<td>Rafferty</td>
<td>31</td>
<td>Engineering</td>
<td>33
</td></tr>
<tr>
<td>Jones</td>
<td>33</td>
<td>Engineering</td>
<td>33
</td></tr>
<tr>
<td>Heisenberg</td>
<td>33</td>
<td>Engineering</td>
<td>33
</td></tr>
<tr>
<td>Smith</td>
<td>34</td>
<td>Engineering</td>
<td>33
</td></tr>
<tr>
<td>Robinson</td>
<td>34</td>
<td>Engineering</td>
<td>33
</td></tr>
<tr>
<td>Williams</td>
<td><code style="color: white; background-color: gray; padding: 2px 4px; font-size: smaller;">NULL</code></td>
<td>Engineering</td>
<td>33
</td></tr>
<tr>
<td>Rafferty</td>
<td>31</td>
<td>Clerical</td>
<td>34
</td></tr>
<tr>
<td>Jones</td>
<td>33</td>
<td>Clerical</td>
<td>34
</td></tr>
<tr>
<td>Heisenberg</td>
<td>33</td>
<td>Clerical</td>
<td>34
</td></tr>
<tr>
<td>Smith</td>
<td>34</td>
<td>Clerical</td>
<td>34
</td></tr>
<tr>
<td>Robinson</td>
<td>34</td>
<td>Clerical</td>
<td>34
</td></tr>
<tr>
<td>Williams</td>
<td><code style="color: white; background-color: gray; padding: 2px 4px; font-size: smaller;">NULL</code></td>
<td>Clerical</td>
<td>34
</td></tr>
<tr>
<td>Rafferty</td>
<td>31</td>
<td>Marketing</td>
<td>35
</td></tr>
<tr>
<td>Jones</td>
<td>33</td>
<td>Marketing</td>
<td>35
</td></tr>
<tr>
<td>Heisenberg</td>
<td>33</td>
<td>Marketing</td>
<td>35
</td></tr>
<tr>
<td>Smith</td>
<td>34</td>
<td>Marketing</td>
<td>35
</td></tr>
<tr>
<td>Robinson</td>
<td>34</td>
<td>Marketing</td>
<td>35
</td></tr>
<tr>
<td>Williams</td>
<td><code style="color: white; background-color: gray; padding: 2px 4px; font-size: smaller;">NULL</code></td>
<td>Marketing</td>
<td>35
</td></tr></tbody></table>
<p>Example of an explicit cross join:
</p>
<div class="mw-highlight mw-highlight-lang-sql mw-content-ltr" dir="ltr"><pre><span class="k">SELECT</span><span class="w"> </span><span class="o">*</span>
<span class="k">FROM</span><span class="w"> </span><span class="n">employee</span><span class="w"> </span><span class="k">CROSS</span><span class="w"> </span><span class="k">JOIN</span><span class="w"> </span><span class="n">department</span><span class="p">;</span>
</pre></div>
<p>Example of an implicit cross join:
</p>
<div class="mw-highlight mw-highlight-lang-sql mw-content-ltr" dir="ltr"><pre><span class="k">SELECT</span><span class="w"> </span><span class="o">*</span>
<span class="k">FROM</span><span class="w"> </span><span class="n">employee</span><span class="p">,</span><span class="w"> </span><span class="n">department</span><span class="p">;</span>
</pre></div><p>The cross join can be replaced with an inner join with an always-true condition:</p><div class="mw-highlight mw-highlight-lang-sql mw-content-ltr" dir="ltr"><pre><span class="k">SELECT</span><span class="w"> </span><span class="o">*</span>
<span class="k">FROM</span><span class="w"> </span><span class="n">employee</span><span class="w"> </span><span class="k">INNER</span><span class="w"> </span><span class="k">JOIN</span><span class="w"> </span><span class="n">department</span><span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="mi">1</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span>
</pre></div>
<p><code>CROSS JOIN</code> does not itself apply any predicate to filter rows from the joined table. The results of a <code>CROSS JOIN</code> can be filtered using a <code>WHERE</code> clause, which may then produce the equivalent of an inner join.
</p><p>In the <a href="SQL%3A2011" title="SQL:2011">SQL:2011</a> standard, cross joins are part of the optional F401, "Extended joined table", package.
</p><p>Normal uses are for checking the server's performance.
</p>
<div class="mw-heading mw-heading2"><h2 id="Inner_join">Inner join</h2></div>
<p>An <b>inner join</b> (or <b>join</b>) requires each row in the two joined tables to have matching column values, and is a commonly used join operation in <a href="Application_software" title="Application software">applications</a> but should not be assumed to be the best choice in all situations. Inner join creates a new result table by combining column values of two tables (A and B) based upon the join-predicate. The query compares each row of A with each row of B to find all pairs of rows that satisfy the join-predicate. When the join-predicate is satisfied by matching non-<a href="Null_(SQL)" title="Null (SQL)">NULL</a> values, column values for each matched pair of rows of A and B are combined into a result row.
</p><p>The result of the join can be defined as the outcome of first taking the <a href="Cartesian_product" title="Cartesian product">cartesian product</a> (or <a href="#Cross_join">cross join</a>) of all rows in the tables (combining every row in table A with every row in table B) and then returning all rows that satisfy the join predicate. Actual SQL implementations normally use other approaches, such as <a href="Hash_join" title="Hash join">hash joins</a> or <a href="Sort-merge_join" title="Sort-merge join">sort-merge joins</a>, since computing the Cartesian product is slower and would often require a prohibitively large amount of memory to store.
</p><p>SQL specifies two different syntactical ways to express joins: the "explicit join notation" and the "implicit join notation". The "implicit join notation" is no longer considered a best practice, although database systems still support it.
</p><p>The "explicit join notation" uses the <code>JOIN</code> keyword, optionally preceded by the <code>INNER</code> keyword, to specify the table to join, and the <code>ON</code> keyword to specify the predicates for the join, as in the following example:
</p>
<div class="mw-highlight mw-highlight-lang-sql mw-content-ltr" dir="ltr"><pre><span class="k">SELECT</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">LastName</span><span class="p">,</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">DepartmentID</span><span class="p">,</span><span class="w"> </span><span class="n">department</span><span class="p">.</span><span class="n">DepartmentName</span><span class="w"> </span>
<span class="k">FROM</span><span class="w"> </span><span class="n">employee</span><span class="w"> </span>
<span class="k">INNER</span><span class="w"> </span><span class="k">JOIN</span><span class="w"> </span><span class="n">department</span><span class="w"> </span><span class="k">ON</span>
<span class="n">employee</span><span class="p">.</span><span class="n">DepartmentID</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">department</span><span class="p">.</span><span class="n">DepartmentID</span><span class="p">;</span>
</pre></div>
<table class="wikitable">
<tbody><tr>
<th>Employee.LastName</th>
<th>Employee.DepartmentID</th>
<th>Department.DepartmentName
</th></tr>
<tr>
<td>Robinson</td>
<td>34</td>
<td>Clerical
</td></tr>
<tr>
<td>Jones</td>
<td>33</td>
<td>Engineering
</td></tr>
<tr>
<td>Smith</td>
<td>34</td>
<td>Clerical
</td></tr>
<tr>
<td>Heisenberg</td>
<td>33</td>
<td>Engineering
</td></tr>
<tr>
<td>Rafferty</td>
<td>31</td>
<td>Sales
</td></tr></tbody></table>
<p>The "implicit join notation" simply lists the tables for joining, in the <code>FROM</code> clause of the <code>SELECT</code> statement, using commas to separate them. Thus it specifies a <a href="#Cross_join">cross join</a>, and the <code>WHERE</code> clause may apply additional filter-predicates (which function comparably to the join-predicates in the explicit notation).
</p><p>The following example is equivalent to the previous one, but this time using implicit join notation:
</p>
<div class="mw-highlight mw-highlight-lang-sql mw-content-ltr" dir="ltr"><pre><span class="k">SELECT</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">LastName</span><span class="p">,</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">DepartmentID</span><span class="p">,</span><span class="w"> </span><span class="n">department</span><span class="p">.</span><span class="n">DepartmentName</span><span class="w"> </span>
<span class="k">FROM</span><span class="w"> </span><span class="n">employee</span><span class="p">,</span><span class="w"> </span><span class="n">department</span>
<span class="k">WHERE</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">DepartmentID</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">department</span><span class="p">.</span><span class="n">DepartmentID</span><span class="p">;</span>
</pre></div>
<p>The queries given in the examples above will join the Employee and department tables using the DepartmentID column of both tables. Where the DepartmentID of these tables match (i.e. the join-predicate is satisfied), the query will combine the <i>LastName</i>, <i>DepartmentID</i> and <i>DepartmentName</i> columns from the two tables into a result row. Where the DepartmentID does not match, no result row is generated.
</p><p>Thus the result of the <a href="Query_plan" title="Query plan">execution</a> of the query above will be:
</p>
<table class="wikitable">
<tbody><tr>
<th>Employee.LastName</th>
<th>Employee.DepartmentID</th>
<th>Department.DepartmentName
</th></tr>
<tr>
<td>Robinson</td>
<td>34</td>
<td>Clerical
</td></tr>
<tr>
<td>Jones</td>
<td>33</td>
<td>Engineering
</td></tr>
<tr>
<td>Smith</td>
<td>34</td>
<td>Clerical
</td></tr>
<tr>
<td>Heisenberg</td>
<td>33</td>
<td>Engineering
</td></tr>
<tr>
<td>Rafferty</td>
<td>31</td>
<td>Sales
</td></tr></tbody></table>
<p>The employee "Williams" and the department "Marketing" do not appear in the query execution results. Neither of these has any matching rows in the other respective table: "Williams" has no associated department, and no employee has the department ID 35 ("Marketing"). Depending on the desired results, this behavior may be a subtle bug, which can be avoided by replacing the inner join with an <a href="#Outer_join">outer join</a>.
</p>
<div class="mw-heading mw-heading3"><h3 id="Inner_join_and_NULL_values">Inner join and NULL values</h3></div>
<p>Programmers should take special care when joining tables on columns that can contain <a href="Null_(SQL)" title="Null (SQL)">NULL</a> values, since NULL will never match any other value (not even NULL itself), unless the join condition explicitly uses a combination predicate that first checks that the joins columns are <code> NOT NULL</code> before applying the remaining predicate condition(s). The Inner Join can only be safely used in a database that enforces <a href="Referential_integrity" title="Referential integrity">referential integrity</a> or where the join columns are guaranteed not to be NULL. Many <a href="Transaction_processing" title="Transaction processing">transaction processing</a> relational databases rely on <a href="ACID" title="ACID">atomicity, consistency, isolation, durability</a> (ACID) data update standards to ensure data integrity, making inner joins an appropriate choice. However, transaction databases usually also have desirable join columns that are allowed to be NULL. Many reporting relational database and <a href="Data_warehouse" title="Data warehouse">data warehouses</a> use high volume <a href="Extract%2C_transform%2C_load" title="Extract, transform, load">extract, transform, load</a> (ETL) batch updates which make referential integrity difficult or impossible to enforce, resulting in potentially NULL join columns that an SQL query author cannot modify and which cause inner joins to omit data with no indication of an error. The choice to use an inner join depends on the database design and data characteristics. A left outer join can usually be substituted for an inner join when the join columns in one table may contain NULL values.
</p><p>Any data column that may be NULL (empty) should never be used as a link in an inner join, unless the intended result is to eliminate the rows with the NULL value. If NULL join columns are to be deliberately removed from the <a href="Result_set" title="Result set">result set</a>, an inner join can be faster than an outer join because the table join and filtering is done in a single step. Conversely, an inner join can result in disastrously slow performance or even a server crash when used in a large volume query in combination with database functions in an SQL Where clause.<sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-4" class="reference"><a href="#cite_note-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup> A function in an SQL Where clause can result in the database ignoring relatively compact table indexes. The database may read and inner join the selected columns from both tables before reducing the number of rows using the filter that depends on a calculated value, resulting in a relatively enormous amount of inefficient processing.
</p><p>When a result set is produced by joining several tables, including master tables used to look up full-text descriptions of numeric identifier codes (a <a href="Lookup_table" title="Lookup table">Lookup table</a>), a NULL value in any one of the foreign keys can result in the entire row being eliminated from the result set, with no indication of error. A complex SQL query that includes one or more inner joins and several outer joins has the same risk for NULL values in the inner join link columns.
</p><p>A commitment to SQL code containing inner joins assumes NULL join columns will not be introduced by future changes, including vendor updates, design changes and bulk processing outside of the application's data validation rules such as data conversions, migrations, bulk imports and merges.
</p><p>One can further classify inner joins as equi-joins, as natural joins, or as cross-joins.
</p>
<div class="mw-heading mw-heading3"><h3 id="Equi-join">Equi-join</h3></div>
<p>The <b>equi-join</b>, also known as "the only eligible operation", is a specific type of comparator-based join, that uses only <a href="Equality_(mathematics)" title="Equality (mathematics)">equality</a> comparisons in the join-predicate. Using other comparison operators (such as <code><</code>) disqualifies a join as an equi-join. The query shown above has already provided an example of an equi-join:
</p>
<div class="mw-highlight mw-highlight-lang-sql mw-content-ltr" dir="ltr"><pre><span class="k">SELECT</span><span class="w"> </span><span class="o">*</span>
<span class="k">FROM</span><span class="w"> </span><span class="n">employee</span><span class="w"> </span><span class="k">JOIN</span><span class="w"> </span><span class="n">department</span>
<span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">DepartmentID</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">department</span><span class="p">.</span><span class="n">DepartmentID</span><span class="p">;</span>
</pre></div>
<p>We can write equi-join as below,
</p>
<div class="mw-highlight mw-highlight-lang-sql mw-content-ltr" dir="ltr"><pre><span class="k">SELECT</span><span class="w"> </span><span class="o">*</span>
<span class="k">FROM</span><span class="w"> </span><span class="n">employee</span><span class="p">,</span><span class="w"> </span><span class="n">department</span>
<span class="k">WHERE</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">DepartmentID</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">department</span><span class="p">.</span><span class="n">DepartmentID</span><span class="p">;</span>
</pre></div>
<p>If columns in an equi-join have the same name, <a href="SQL-92" title="SQL-92">SQL-92</a> provides an optional shorthand notation for expressing equi-joins, by way of the <code>USING</code> construct:<sup id="cite_ref-5" class="reference"><a href="#cite_note-5"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-highlight mw-highlight-lang-sql mw-content-ltr" dir="ltr"><pre><span class="k">SELECT</span><span class="w"> </span><span class="o">*</span>
<span class="k">FROM</span><span class="w"> </span><span class="n">employee</span><span class="w"> </span><span class="k">INNER</span><span class="w"> </span><span class="k">JOIN</span><span class="w"> </span><span class="n">department</span><span class="w"> </span><span class="k">USING</span><span class="w"> </span><span class="p">(</span><span class="n">DepartmentID</span><span class="p">);</span>
</pre></div>
<p>The <code>USING</code> construct is more than mere <a href="Syntactic_sugar" title="Syntactic sugar">syntactic sugar</a>, however, since the result set differs from the result set of the version with the explicit predicate. Specifically, any columns mentioned in the <code>USING</code> list will appear only once, with an unqualified name, rather than once for each table in the join. In the case above, there will be a single <code>DepartmentID</code> column and no <code>employee.DepartmentID</code> or <code>department.DepartmentID</code>.
</p><p>The <code>USING</code> clause is not supported by MS SQL Server and Sybase.
</p>
<div class="mw-heading mw-heading4"><h4 id="Natural_join">Natural join</h4></div>
<p>The natural join is a special case of equi-join. Natural join (⋈) is a <a href="Binary_relation" title="Binary relation">binary operator</a> that is written as (<i>R</i> ⋈ <i>S</i>) where <i>R</i> and <i>S</i> are <a href="Relation_(database)" title="Relation (database)">relations</a>.<sup id="cite_ref-6" class="reference"><a href="#cite_note-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup> The result of the natural join is the set of all combinations of <a href="Tuples" class="mw-redirect" title="Tuples">tuples</a> in <i>R</i> and <i>S</i> that are equal on their common attribute names. For an example consider the tables <i>Employee</i> and <i>Dept</i> and their natural join:
</p>
<table style="margin: 0 auto;" cellpadding="20">
<tbody><tr valign="top">
<td>
<table class="wikitable">
<caption><i>Employee</i>
</caption>
<tbody><tr>
<th>Name</th>
<th>EmpId</th>
<th>DeptName
</th></tr>
<tr>
<td>Harry</td>
<td>3415</td>
<td>Finance
</td></tr>
<tr>
<td>Sally</td>
<td>2241</td>
<td>Sales
</td></tr>
<tr>
<td>George</td>
<td>3401</td>
<td>Finance
</td></tr>
<tr>
<td>Harriet</td>
<td>2202</td>
<td>Sales
</td></tr></tbody></table>
</td>
<td>
<table class="wikitable">
<caption><i>Dept</i>
</caption>
<tbody><tr>
<th>DeptName</th>
<th>Manager
</th></tr>
<tr>
<td>Finance</td>
<td>George
</td></tr>
<tr>
<td>Sales</td>
<td>Harriet
</td></tr>
<tr>
<td>Production</td>
<td>Charles
</td></tr></tbody></table>
</td>
<td>
<table class="wikitable">
<caption><i>Employee</i> <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle \bowtie }">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mo>⋈<!-- ⋈ --></mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle \bowtie }</annotation>
</semantics>
</math></span><img src="./bc29b9e87e8e88d9e1f70a95c08676c3e59976e0.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:2.091ex; height:1.843ex;" alt="{\displaystyle \bowtie }" loading="lazy"></span> <i>Dept</i>
</caption>
<tbody><tr>
<th>Name</th>
<th>EmpId</th>
<th>DeptName</th>
<th>Manager
</th></tr>
<tr>
<td>Harry</td>
<td>3415</td>
<td>Finance</td>
<td>George
</td></tr>
<tr>
<td>Sally</td>
<td>2241</td>
<td>Sales</td>
<td>Harriet
</td></tr>
<tr>
<td>George</td>
<td>3401</td>
<td>Finance</td>
<td>George
</td></tr>
<tr>
<td>Harriet</td>
<td>2202</td>
<td>Sales</td>
<td>Harriet
</td></tr></tbody></table>
</td></tr></tbody></table>
<p>This can also be used to define <a href="Composition_of_relations" title="Composition of relations">composition of relations</a>. For example, the composition of <i>Employee</i> and <i>Dept</i> is their join as shown above, projected on all but the common attribute <i>DeptName</i>. In <a href="Category_theory" title="Category theory">category theory</a>, the join is precisely the <a href="Fiber_product" class="mw-redirect" title="Fiber product">fiber product</a>.
</p><p>The natural join is arguably one of the most important operators since it is the relational counterpart of logical AND. Note that if the same variable appears in each of two predicates that are connected by AND, then that variable stands for the same thing and both appearances must always be substituted by the same value. In particular, the natural join allows the combination of relations that are associated by a <a href="Foreign_key" title="Foreign key">foreign key</a>. For example, in the above example a foreign key probably holds from <i>Employee</i>.<i>DeptName</i> to <i>Dept</i>.<i>DeptName</i> and then the natural join of <i>Employee</i> and <i>Dept</i> combines all employees with their departments. This works because the foreign key holds between attributes with the same name. If this is not the case such as in the foreign key from <i>Dept</i>.<i>manager</i> to <i>Employee</i>.<i>Name</i> then these columns have to be renamed before the natural join is taken. Such a join is sometimes also referred to as an <b>equi-join</b>.
</p><p>More formally the semantics of the natural join are defined as follows:
</p>
<dl><dd><span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle R\bowtie S=\left\{t\cup s\mid t\in R\ \land \ s\in S\ \land \ {\mathit {Fun}}(t\cup s)\right\}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>R</mi>
<mo>⋈<!-- ⋈ --></mo>
<mi>S</mi>
<mo>=</mo>
<mrow>
<mo>{</mo>
<mrow>
<mi>t</mi>
<mo>∪<!-- ∪ --></mo>
<mi>s</mi>
<mo>∣<!-- ∣ --></mo>
<mi>t</mi>
<mo>∈<!-- ∈ --></mo>
<mi>R</mi>
<mtext> </mtext>
<mo>∧<!-- ∧ --></mo>
<mtext> </mtext>
<mi>s</mi>
<mo>∈<!-- ∈ --></mo>
<mi>S</mi>
<mtext> </mtext>
<mo>∧<!-- ∧ --></mo>
<mtext> </mtext>
<mrow class="MJX-TeXAtom-ORD">
<mrow class="MJX-TeXAtom-ORD">
<mi class="MJX-tex-mathit" mathvariant="italic">F</mi>
<mi class="MJX-tex-mathit" mathvariant="italic">u</mi>
<mi class="MJX-tex-mathit" mathvariant="italic">n</mi>
</mrow>
</mrow>
<mo stretchy="false">(</mo>
<mi>t</mi>
<mo>∪<!-- ∪ --></mo>
<mi>s</mi>
<mo stretchy="false">)</mo>
</mrow>
<mo>}</mo>
</mrow>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle R\bowtie S=\left\{t\cup s\mid t\in R\ \land \ s\in S\ \land \ {\mathit {Fun}}(t\cup s)\right\}}</annotation>
</semantics>
</math></span><img src="./1a32d7c5edd7c2cf320c29baa98fd856b0d87d60.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:47.275ex; height:2.843ex;" alt="{\displaystyle R\bowtie S=\left\{t\cup s\mid t\in R\ \land \ s\in S\ \land \ {\mathit {Fun}}(t\cup s)\right\}}" loading="lazy"></span>,</dd></dl>
<p>where <i>Fun</i> is a <a href="Predicate_(mathematics)" class="mw-redirect" title="Predicate (mathematics)">predicate</a> that is true for a <a href="Relation_(mathematics)" title="Relation (mathematics)">relation</a> <i>r</i> <a href="If_and_only_if" title="If and only if">if and only if</a> <i>r</i> is a function. It is usually required that <i>R</i> and <i>S</i> must have at least one common attribute, but if this constraint is omitted, and <i>R</i> and <i>S</i> have no common attributes, then the natural join becomes exactly the Cartesian product.
</p><p>The natural join can be simulated with Codd's primitives as follows. Let <i>c</i><sub>1</sub>, ..., <i>c</i><sub><i>m</i></sub> be the attribute names common to <i>R</i> and <i>S</i>, <i>r</i><sub>1</sub>, ..., <i>r</i><sub><i>n</i></sub> be the attribute names unique to <i>R</i> and let <i>s</i><sub>1</sub>, ..., <i>s</i><sub><i>k</i></sub> be the attributes unique to <i>S</i>. Furthermore, assume that the attribute names <i>x</i><sub>1</sub>, ..., <i>x</i><sub><i>m</i></sub> are neither in <i>R</i> nor in <i>S</i>. In a first step the common attribute names in <i>S</i> can now be renamed:
</p>
<dl><dd><span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle T=\rho _{x_{1}/c_{1},\ldots ,x_{m}/c_{m}}(S)=\rho _{x_{1}/c_{1}}(\rho _{x_{2}/c_{2}}(\ldots \rho _{x_{m}/c_{m}}(S)\ldots ))}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>T</mi>
<mo>=</mo>
<msub>
<mi>ρ<!-- ρ --></mi>
<mrow class="MJX-TeXAtom-ORD">
<msub>
<mi>x</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>1</mn>
</mrow>
</msub>
<mrow class="MJX-TeXAtom-ORD">
<mo>/</mo>
</mrow>
<msub>
<mi>c</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>1</mn>
</mrow>
</msub>
<mo>,</mo>
<mo>…<!-- … --></mo>
<mo>,</mo>
<msub>
<mi>x</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>m</mi>
</mrow>
</msub>
<mrow class="MJX-TeXAtom-ORD">
<mo>/</mo>
</mrow>
<msub>
<mi>c</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>m</mi>
</mrow>
</msub>
</mrow>
</msub>
<mo stretchy="false">(</mo>
<mi>S</mi>
<mo stretchy="false">)</mo>
<mo>=</mo>
<msub>
<mi>ρ<!-- ρ --></mi>
<mrow class="MJX-TeXAtom-ORD">
<msub>
<mi>x</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>1</mn>
</mrow>
</msub>
<mrow class="MJX-TeXAtom-ORD">
<mo>/</mo>
</mrow>
<msub>
<mi>c</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>1</mn>
</mrow>
</msub>
</mrow>
</msub>
<mo stretchy="false">(</mo>
<msub>
<mi>ρ<!-- ρ --></mi>
<mrow class="MJX-TeXAtom-ORD">
<msub>
<mi>x</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</msub>
<mrow class="MJX-TeXAtom-ORD">
<mo>/</mo>
</mrow>
<msub>
<mi>c</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
</mrow>
</msub>
</mrow>
</msub>
<mo stretchy="false">(</mo>
<mo>…<!-- … --></mo>
<msub>
<mi>ρ<!-- ρ --></mi>
<mrow class="MJX-TeXAtom-ORD">
<msub>
<mi>x</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>m</mi>
</mrow>
</msub>
<mrow class="MJX-TeXAtom-ORD">
<mo>/</mo>
</mrow>
<msub>
<mi>c</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>m</mi>
</mrow>
</msub>
</mrow>
</msub>
<mo stretchy="false">(</mo>
<mi>S</mi>
<mo stretchy="false">)</mo>
<mo>…<!-- … --></mo>
<mo stretchy="false">)</mo>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle T=\rho _{x_{1}/c_{1},\ldots ,x_{m}/c_{m}}(S)=\rho _{x_{1}/c_{1}}(\rho _{x_{2}/c_{2}}(\ldots \rho _{x_{m}/c_{m}}(S)\ldots ))}</annotation>
</semantics>
</math></span><img src="./5c591661e5ae892fb99445827839f9b208846f67.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -1.171ex; width:55.569ex; height:3.176ex;" alt="{\displaystyle T=\rho _{x_{1}/c_{1},\ldots ,x_{m}/c_{m}}(S)=\rho _{x_{1}/c_{1}}(\rho _{x_{2}/c_{2}}(\ldots \rho _{x_{m}/c_{m}}(S)\ldots ))}" loading="lazy"></span></dd></dl>
<p>Then we take the Cartesian product and select the tuples that are to be joined:
</p>
<dl><dd><span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle U=\pi _{r_{1},\ldots ,r_{n},c_{1},\ldots ,c_{m},s_{1},\ldots ,s_{k}}(P)}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>U</mi>
<mo>=</mo>
<msub>
<mi>π<!-- π --></mi>
<mrow class="MJX-TeXAtom-ORD">
<msub>
<mi>r</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>1</mn>
</mrow>
</msub>
<mo>,</mo>
<mo>…<!-- … --></mo>
<mo>,</mo>
<msub>
<mi>r</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
</mrow>
</msub>
<mo>,</mo>
<msub>
<mi>c</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>1</mn>
</mrow>
</msub>
<mo>,</mo>
<mo>…<!-- … --></mo>
<mo>,</mo>
<msub>
<mi>c</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>m</mi>
</mrow>
</msub>
<mo>,</mo>
<msub>
<mi>s</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>1</mn>
</mrow>
</msub>
<mo>,</mo>
<mo>…<!-- … --></mo>
<mo>,</mo>
<msub>
<mi>s</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>k</mi>
</mrow>
</msub>
</mrow>
</msub>
<mo stretchy="false">(</mo>
<mi>P</mi>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle U=\pi _{r_{1},\ldots ,r_{n},c_{1},\ldots ,c_{m},s_{1},\ldots ,s_{k}}(P)}</annotation>
</semantics>
</math></span><img src="./ec46ccaf1759695d27a262472a23ad8b118ea83d.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -1.005ex; width:29.533ex; height:3.009ex;" alt="{\displaystyle U=\pi _{r_{1},\ldots ,r_{n},c_{1},\ldots ,c_{m},s_{1},\ldots ,s_{k}}(P)}" loading="lazy"></span></dd></dl>
<p>A <a href="Natural_join" class="mw-redirect" title="Natural join">natural join</a> is a type of equi-join where the <b>join</b> predicate arises implicitly by comparing all columns in both tables that have the same column-names in the joined tables. The resulting joined table contains only one column for each pair of equally named columns. In the case that no columns with the same names are found, the result is a <a href="Cross_join" class="mw-redirect" title="Cross join">cross join</a>.
</p><p>Most experts agree that NATURAL JOINs are dangerous and therefore strongly discourage their use.<sup id="cite_ref-7" class="reference"><a href="#cite_note-7"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup> The danger comes from inadvertently adding a new column, named the same as another column in the other table. An existing natural join might then "naturally" use the new column for comparisons, making comparisons/matches using different criteria (from different columns) than before. Thus an existing query could produce different results, even though the data in the tables have not been changed, but only augmented. The use of column names to automatically determine table links is not an option in large databases with hundreds or thousands of tables where it would place an unrealistic constraint on naming conventions. Real world databases are commonly designed with <a href="Foreign_key" title="Foreign key">foreign key</a> data that is not consistently populated (NULL values are allowed), due to business rules and context. It is common practice to modify column names of similar data in different tables and this lack of rigid consistency relegates natural joins to a theoretical concept for discussion.
</p><p>The above sample query for inner joins can be expressed as a natural join in the following way:
</p>
<div class="mw-highlight mw-highlight-lang-sql mw-content-ltr" dir="ltr"><pre><span class="k">SELECT</span><span class="w"> </span><span class="o">*</span>
<span class="k">FROM</span><span class="w"> </span><span class="n">employee</span><span class="w"> </span><span class="k">NATURAL</span><span class="w"> </span><span class="k">JOIN</span><span class="w"> </span><span class="n">department</span><span class="p">;</span>
</pre></div>
<p>As with the explicit <code>USING</code> clause, only one DepartmentID column occurs in the joined table, with no qualifier:
</p>
<table class="wikitable" style="text-align:center">
<tbody><tr>
<th>DepartmentID</th>
<th>Employee.LastName</th>
<th>Department.DepartmentName
</th></tr>
<tr>
<td>34</td>
<td>Smith</td>
<td>Clerical
</td></tr>
<tr>
<td>33</td>
<td>Jones</td>
<td>Engineering
</td></tr>
<tr>
<td>34</td>
<td>Robinson</td>
<td>Clerical
</td></tr>
<tr>
<td>33</td>
<td>Heisenberg</td>
<td>Engineering
</td></tr>
<tr>
<td>31</td>
<td>Rafferty</td>
<td>Sales
</td></tr></tbody></table>
<p>PostgreSQL, MySQL and Oracle support natural joins; Microsoft T-SQL and IBM DB2 do not. The columns used in the join are implicit so the join code does not show which columns are expected, and a change in column names may change the results. In the <a href="SQL%3A2011" title="SQL:2011">SQL:2011</a> standard, natural joins are part of the optional F401, "Extended joined table", package.
</p><p>In many database environments the column names are controlled by an outside vendor, not the query developer. A natural join assumes stability and consistency in column names which can change during vendor mandated version upgrades.
</p>
<div class="mw-heading mw-heading2"><h2 id="Outer_join">Outer join</h2></div>
<p>The joined table retains each row—even if no other matching row exists. Outer joins subdivide further into left outer joins, right outer joins, and full outer joins, depending on which table's rows are retained: left, right, or both (in this case <i>left</i> and <i>right</i> refer to the two sides of the <code>JOIN</code> keyword). Like <a href="#Inner_join">inner joins</a>, one can further sub-categorize all types of outer joins as <a href="#Equi-join">equi-joins</a>, <a href="#Natural_join">natural joins</a>, <code><b>ON</b> <i><predicate></i></code> (<a href="Relational_algebra#θ-join_and_equijoin" title="Relational algebra"><i>θ</i>-join</a>), etc.<sup id="cite_ref-8" class="reference"><a href="#cite_note-8"><span class="cite-bracket">[</span>8<span class="cite-bracket">]</span></a></sup>
</p><p>No implicit join-notation for outer joins exists in standard SQL.
</p>
<div class="mw-heading mw-heading3"><h3 id="Left_outer_join">Left outer join</h3></div>
<p>The result of a <b>left outer join</b> (or simply <b>left join</b>) for tables A and B always contains all rows of the "left" table (A), even if the join-condition does not find any matching row in the "right" table (B). This means that if the <code>ON</code> clause matches 0 (zero) rows in B (for a given row in A), the join will still return a row in the result (for that row)—but with NULL in each column from B. A <b>left outer join</b> returns all the values from an inner join plus all values in the left table that do not match to the right table, including rows with NULL (empty) values in the link column.
</p><p>For example, this allows us to find an employee's department, but still shows employees that have not been assigned to a department (contrary to the inner-join example above, where unassigned employees were excluded from the result).
</p><p>Example of a left outer join (the <b><code>OUTER</code></b> keyword is optional), with the additional result row (compared with the inner join) italicized:
</p>
<div class="mw-highlight mw-highlight-lang-sql mw-content-ltr" dir="ltr"><pre><span class="k">SELECT</span><span class="w"> </span><span class="o">*</span>
<span class="k">FROM</span><span class="w"> </span><span class="n">employee</span><span class="w"> </span>
<span class="k">LEFT</span><span class="w"> </span><span class="k">OUTER</span><span class="w"> </span><span class="k">JOIN</span><span class="w"> </span><span class="n">department</span><span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">DepartmentID</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">department</span><span class="p">.</span><span class="n">DepartmentID</span><span class="p">;</span>
</pre></div>
<table class="wikitable" style="text-align:center">
<tbody><tr>
<th>Employee.LastName</th>
<th>Employee.DepartmentID</th>
<th>Department.DepartmentName</th>
<th>Department.DepartmentID
</th></tr>
<tr>
<td>Jones</td>
<td>33</td>
<td>Engineering</td>
<td>33
</td></tr>
<tr>
<td>Rafferty</td>
<td>31</td>
<td>Sales</td>
<td>31
</td></tr>
<tr>
<td>Robinson</td>
<td>34</td>
<td>Clerical</td>
<td>34
</td></tr>
<tr>
<td>Smith</td>
<td>34</td>
<td>Clerical</td>
<td>34
</td></tr>
<tr>
<td><i>Williams</i></td>
<td><code style="color: white; background-color: gray; padding: 2px 4px; font-size: smaller;">NULL</code></td>
<td><code style="color: white; background-color: gray; padding: 2px 4px; font-size: smaller;">NULL</code></td>
<td><code style="color: white; background-color: gray; padding: 2px 4px; font-size: smaller;">NULL</code>
</td></tr>
<tr>
<td>Heisenberg</td>
<td>33</td>
<td>Engineering</td>
<td>33
</td></tr></tbody></table>
<div class="mw-heading mw-heading4"><h4 id="Alternative_syntaxes">Alternative syntaxes</h4></div>
<p>Oracle supports the deprecated<sup id="cite_ref-deprecated_plus_sign_9-0" class="reference"><a href="#cite_note-deprecated_plus_sign-9"><span class="cite-bracket">[</span>9<span class="cite-bracket">]</span></a></sup> syntax:
</p>
<div class="mw-highlight mw-highlight-lang-sql mw-content-ltr" dir="ltr"><pre><span class="k">SELECT</span><span class="w"> </span><span class="o">*</span>
<span class="k">FROM</span><span class="w"> </span><span class="n">employee</span><span class="p">,</span><span class="w"> </span><span class="n">department</span>
<span class="k">WHERE</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">DepartmentID</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">department</span><span class="p">.</span><span class="n">DepartmentID</span><span class="p">(</span><span class="o">+</span><span class="p">)</span>
</pre></div>
<p><a href="Sybase" title="Sybase">Sybase</a> supports the syntax (<a href="Microsoft_SQL_Server" title="Microsoft SQL Server">Microsoft SQL Server</a> deprecated this syntax since version 2000):
</p>
<div class="mw-highlight mw-highlight-lang-tsql mw-content-ltr" dir="ltr"><pre><span class="k">SELECT</span><span class="w"> </span><span class="o">*</span>
<span class="k">FROM</span><span class="w"> </span><span class="n">employee</span><span class="p">,</span><span class="w"> </span><span class="n">department</span>
<span class="k">WHERE</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">DepartmentID</span><span class="w"> </span><span class="o">*=</span><span class="w"> </span><span class="n">department</span><span class="p">.</span><span class="n">DepartmentID</span>
</pre></div>
<p><a href="IBM_Informix" class="mw-redirect" title="IBM Informix">IBM Informix</a> supports the syntax:
</p>
<div class="mw-highlight mw-highlight-lang-sql mw-content-ltr" dir="ltr"><pre><span class="k">SELECT</span><span class="w"> </span><span class="o">*</span>
<span class="k">FROM</span><span class="w"> </span><span class="n">employee</span><span class="p">,</span><span class="w"> </span><span class="k">OUTER</span><span class="w"> </span><span class="n">department</span>
<span class="k">WHERE</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">DepartmentID</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">department</span><span class="p">.</span><span class="n">DepartmentID</span>
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="Right_outer_join">Right outer join</h3></div>
<p>A <b>right outer join</b> (or <b>right join</b>) closely resembles a left outer join, except with the treatment of the tables reversed. Every row from the "right" table (B) will appear in the joined table at least once. If no matching row from the "left" table (A) exists, NULL will appear in columns from A for those rows that have no match in B.
</p><p>A right outer join returns all the values from the right table and matched values from the left table (NULL in the case of no matching join predicate). For example, this allows us to find each employee and his or her department, but still show departments that have no employees.
</p><p>Below is an example of a right outer join (the <b><code>OUTER</code></b> keyword is optional), with the additional result row italicized:
</p>
<div class="mw-highlight mw-highlight-lang-sql mw-content-ltr" dir="ltr"><pre><span class="k">SELECT</span><span class="w"> </span><span class="o">*</span>
<span class="k">FROM</span><span class="w"> </span><span class="n">employee</span><span class="w"> </span><span class="k">RIGHT</span><span class="w"> </span><span class="k">OUTER</span><span class="w"> </span><span class="k">JOIN</span><span class="w"> </span><span class="n">department</span>
<span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">DepartmentID</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">department</span><span class="p">.</span><span class="n">DepartmentID</span><span class="p">;</span>
</pre></div>
<table class="wikitable" style="text-align:center">
<tbody><tr>
<th>Employee.LastName</th>
<th>Employee.DepartmentID</th>
<th>Department.DepartmentName</th>
<th>Department.DepartmentID
</th></tr>
<tr>
<td>Smith</td>
<td>34</td>
<td>Clerical</td>
<td>34
</td></tr>
<tr>
<td>Jones</td>
<td>33</td>
<td>Engineering</td>
<td>33
</td></tr>
<tr>
<td>Robinson</td>
<td>34</td>
<td>Clerical</td>
<td>34
</td></tr>
<tr>
<td>Heisenberg</td>
<td>33</td>
<td>Engineering</td>
<td>33
</td></tr>
<tr>
<td>Rafferty</td>
<td>31</td>
<td>Sales</td>
<td>31
</td></tr>
<tr>
<td><code style="color: white; background-color: gray; padding: 2px 4px; font-size: smaller;">NULL</code></td>
<td><code style="color: white; background-color: gray; padding: 2px 4px; font-size: smaller;">NULL</code></td>
<td><i>Marketing</i></td>
<td><i>35</i>
</td></tr></tbody></table>
<p>Right and left outer joins are functionally equivalent. Neither provides any functionality that the other does not, so right and left outer joins may replace each other as long as the table order is switched.
</p>
<div class="mw-heading mw-heading3"><h3 id="Full_outer_join">Full outer join</h3></div>
<p>Conceptually, a <b>full outer join</b> combines the effect of applying both left and right outer joins. Where rows in the full outer joined tables do not match, the result set will have NULL values for every column of the table that lacks a matching row. For those rows that do match, a single row will be produced in the result set (containing columns populated from both tables).
</p><p>For example, this allows us to see each employee who is in a department and each department that has an employee, but also see each employee who is not part of a department and each department which doesn't have an employee.
</p><p>Example of a full outer join (the <b><code>OUTER</code></b> keyword is optional):
</p>
<div class="mw-highlight mw-highlight-lang-sql mw-content-ltr" dir="ltr"><pre><span class="k">SELECT</span><span class="w"> </span><span class="o">*</span>
<span class="k">FROM</span><span class="w"> </span><span class="n">employee</span><span class="w"> </span><span class="k">FULL</span><span class="w"> </span><span class="k">OUTER</span><span class="w"> </span><span class="k">JOIN</span><span class="w"> </span><span class="n">department</span>
<span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">DepartmentID</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">department</span><span class="p">.</span><span class="n">DepartmentID</span><span class="p">;</span>
</pre></div>
<table class="wikitable" style="text-align:center">
<tbody><tr>
<th>Employee.LastName</th>
<th>Employee.DepartmentID</th>
<th>Department.DepartmentName</th>
<th>Department.DepartmentID
</th></tr>
<tr>
<td>Smith</td>
<td>34</td>
<td>Clerical</td>
<td>34
</td></tr>
<tr>
<td>Jones</td>
<td>33</td>
<td>Engineering</td>
<td>33
</td></tr>
<tr>
<td>Robinson</td>
<td>34</td>
<td>Clerical</td>
<td>34
</td></tr>
<tr>
<td><i>Williams</i></td>
<td><code style="color: white; background-color: gray; padding: 2px 4px; font-size: smaller;">NULL</code></td>
<td><code style="color: white; background-color: gray; padding: 2px 4px; font-size: smaller;">NULL</code></td>
<td><code style="color: white; background-color: gray; padding: 2px 4px; font-size: smaller;">NULL</code>
</td></tr>
<tr>
<td>Heisenberg</td>
<td>33</td>
<td>Engineering</td>
<td>33
</td></tr>
<tr>
<td>Rafferty</td>
<td>31</td>
<td>Sales</td>
<td>31
</td></tr>
<tr>
<td><code style="color: white; background-color: gray; padding: 2px 4px; font-size: smaller;">NULL</code></td>
<td><code style="color: white; background-color: gray; padding: 2px 4px; font-size: smaller;">NULL</code></td>
<td><i>Marketing</i></td>
<td><i>35</i>
</td></tr></tbody></table>
<p>Some database systems do not support the full outer join functionality directly, but they can emulate it through the use of an inner join and UNION ALL selects of the "single table rows" from left and right tables respectively. The same example can appear as follows:
</p>
<div class="mw-highlight mw-highlight-lang-sql mw-content-ltr" dir="ltr"><pre><span class="k">SELECT</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">LastName</span><span class="p">,</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">DepartmentID</span><span class="p">,</span>
<span class="w"> </span><span class="n">department</span><span class="p">.</span><span class="n">DepartmentName</span><span class="p">,</span><span class="w"> </span><span class="n">department</span><span class="p">.</span><span class="n">DepartmentID</span>
<span class="k">FROM</span><span class="w"> </span><span class="n">employee</span>
<span class="k">INNER</span><span class="w"> </span><span class="k">JOIN</span><span class="w"> </span><span class="n">department</span><span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">DepartmentID</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">department</span><span class="p">.</span><span class="n">DepartmentID</span>
<span class="k">UNION</span><span class="w"> </span><span class="k">ALL</span>
<span class="k">SELECT</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">LastName</span><span class="p">,</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">DepartmentID</span><span class="p">,</span>
<span class="w"> </span><span class="k">cast</span><span class="p">(</span><span class="k">NULL</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nb">varchar</span><span class="p">(</span><span class="mi">20</span><span class="p">)),</span><span class="w"> </span><span class="k">cast</span><span class="p">(</span><span class="k">NULL</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nb">integer</span><span class="p">)</span>
<span class="k">FROM</span><span class="w"> </span><span class="n">employee</span>
<span class="k">WHERE</span><span class="w"> </span><span class="k">NOT</span><span class="w"> </span><span class="k">EXISTS</span><span class="w"> </span><span class="p">(</span>
<span class="w"> </span><span class="k">SELECT</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">department</span>
<span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">DepartmentID</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">department</span><span class="p">.</span><span class="n">DepartmentID</span><span class="p">)</span>
<span class="k">UNION</span><span class="w"> </span><span class="k">ALL</span>
<span class="k">SELECT</span><span class="w"> </span><span class="k">cast</span><span class="p">(</span><span class="k">NULL</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nb">varchar</span><span class="p">(</span><span class="mi">20</span><span class="p">)),</span><span class="w"> </span><span class="k">cast</span><span class="p">(</span><span class="k">NULL</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nb">integer</span><span class="p">),</span>
<span class="w"> </span><span class="n">department</span><span class="p">.</span><span class="n">DepartmentName</span><span class="p">,</span><span class="w"> </span><span class="n">department</span><span class="p">.</span><span class="n">DepartmentID</span>
<span class="k">FROM</span><span class="w"> </span><span class="n">department</span>
<span class="k">WHERE</span><span class="w"> </span><span class="k">NOT</span><span class="w"> </span><span class="k">EXISTS</span><span class="w"> </span><span class="p">(</span>
<span class="w"> </span><span class="k">SELECT</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">employee</span>
<span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">DepartmentID</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">department</span><span class="p">.</span><span class="n">DepartmentID</span><span class="p">)</span>
</pre></div>
<p>Another approach could be UNION ALL of left outer join and right outer join MINUS inner join.
</p>
<div class="mw-heading mw-heading2"><h2 id="Self-join">Self-join</h2></div>
<p>A self-join is joining a table to itself.<sup id="cite_ref-10" class="reference"><a href="#cite_note-10"><span class="cite-bracket">[</span>10<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading3"><h3 id="Example">Example</h3></div>
<p>If there were two separate tables for employees and a query which requested employees in the first table having the same country as employees in the second table, a normal join operation could be used to find the answer table. However, all the employee information is contained within a single large table.<sup id="cite_ref-11" class="reference"><a href="#cite_note-11"><span class="cite-bracket">[</span>11<span class="cite-bracket">]</span></a></sup>
</p><p>Consider a modified <code>Employee</code> table such as the following:
</p>
<table class="wikitable" style="text-align:center; float:left; margin-right:5px">
<caption>Employee Table
</caption>
<tbody><tr>
<th>EmployeeID</th>
<th>LastName</th>
<th>Country</th>
<th>DepartmentID
</th></tr>
<tr>
<td>123</td>
<td>Rafferty</td>
<td>Australia</td>
<td>31
</td></tr>
<tr>
<td>124</td>
<td>Jones</td>
<td>Australia</td>
<td>33
</td></tr>
<tr>
<td>145</td>
<td>Heisenberg</td>
<td>Australia</td>
<td>33
</td></tr>
<tr>
<td>201</td>
<td>Robinson</td>
<td>United States</td>
<td>34
</td></tr>
<tr>
<td>305</td>
<td>Smith</td>
<td>Germany</td>
<td>34
</td></tr>
<tr>
<td>306</td>
<td>Williams</td>
<td>Germany</td>
<td><code style="color: white; background-color: gray; padding: 2px 4px; font-size: smaller;">NULL</code>
</td></tr></tbody></table>
<div style="clear:both;" class=""></div>
<p>An example solution query could be as follows:
</p>
<div class="mw-highlight mw-highlight-lang-sql mw-content-ltr" dir="ltr"><pre><span class="k">SELECT</span><span class="w"> </span><span class="n">F</span><span class="p">.</span><span class="n">EmployeeID</span><span class="p">,</span><span class="w"> </span><span class="n">F</span><span class="p">.</span><span class="n">LastName</span><span class="p">,</span><span class="w"> </span><span class="n">S</span><span class="p">.</span><span class="n">EmployeeID</span><span class="p">,</span><span class="w"> </span><span class="n">S</span><span class="p">.</span><span class="n">LastName</span><span class="p">,</span><span class="w"> </span><span class="n">F</span><span class="p">.</span><span class="n">Country</span>
<span class="k">FROM</span><span class="w"> </span><span class="n">Employee</span><span class="w"> </span><span class="n">F</span><span class="w"> </span><span class="k">INNER</span><span class="w"> </span><span class="k">JOIN</span><span class="w"> </span><span class="n">Employee</span><span class="w"> </span><span class="n">S</span><span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="n">F</span><span class="p">.</span><span class="n">Country</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">S</span><span class="p">.</span><span class="n">Country</span>
<span class="k">WHERE</span><span class="w"> </span><span class="n">F</span><span class="p">.</span><span class="n">EmployeeID</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">S</span><span class="p">.</span><span class="n">EmployeeID</span>
<span class="k">ORDER</span><span class="w"> </span><span class="k">BY</span><span class="w"> </span><span class="n">F</span><span class="p">.</span><span class="n">EmployeeID</span><span class="p">,</span><span class="w"> </span><span class="n">S</span><span class="p">.</span><span class="n">EmployeeID</span><span class="p">;</span>
</pre></div>
<p>Which results in the following table being generated.
</p>
<table class="wikitable" style="text-align:center; float:left; margin-right:5px">
<caption>Employee Table after Self-join by Country
</caption>
<tbody><tr>
<th>EmployeeID</th>
<th>LastName</th>
<th>EmployeeID</th>
<th>LastName</th>
<th>Country
</th></tr>
<tr>
<td>123</td>
<td>Rafferty</td>
<td>124</td>
<td>Jones</td>
<td>Australia
</td></tr>
<tr>
<td>123</td>
<td>Rafferty</td>
<td>145</td>
<td>Heisenberg</td>
<td>Australia
</td></tr>
<tr>
<td>124</td>
<td>Jones</td>
<td>145</td>
<td>Heisenberg</td>
<td>Australia
</td></tr>
<tr>
<td>305</td>
<td>Smith</td>
<td>306</td>
<td>Williams</td>
<td>Germany
</td></tr></tbody></table>
<div style="clear:both;" class=""></div>
<p>For this example:
</p>
<ul><li><code>F</code> and <code>S</code> are <a href="Alias_(SQL)" title="Alias (SQL)">aliases</a> for the first and second copies of the employee table.</li>
<li>The condition <code>F.Country = S.Country</code> excludes pairings between employees in different countries. The example question only wanted pairs of employees in the same country.</li>
<li>The condition <code>F.EmployeeID < S.EmployeeID</code> excludes pairings where the <code>EmployeeID</code> of the first employee is greater than or equal to the <code>EmployeeID</code> of the second employee. In other words, the effect of this condition is to exclude duplicate pairings and self-pairings. Without it, the following less useful table would be generated (the table below displays only the "Germany" portion of the result):</li></ul>
<table class="wikitable" style="text-align:center; float:left; margin-right:5px">
<tbody><tr>
<th>EmployeeID</th>
<th>LastName</th>
<th>EmployeeID</th>
<th>LastName</th>
<th>Country
</th></tr>
<tr>
<td>305</td>
<td>Smith</td>
<td>305</td>
<td>Smith</td>
<td>Germany
</td></tr>
<tr>
<td>305</td>
<td>Smith</td>
<td>306</td>
<td>Williams</td>
<td>Germany
</td></tr>
<tr>
<td>306</td>
<td>Williams</td>
<td>305</td>
<td>Smith</td>
<td>Germany
</td></tr>
<tr>
<td>306</td>
<td>Williams</td>
<td>306</td>
<td>Williams</td>
<td>Germany
</td></tr></tbody></table>
<div style="clear:both;" class=""></div>
<p>Only one of the two middle pairings is needed to satisfy the original question, and the topmost and bottommost are of no interest at all in this example.
</p>
<div class="mw-heading mw-heading2"><h2 id="Alternatives">Alternatives</h2></div>
<p>The effect of an outer join can also be obtained using a UNION ALL between an INNER JOIN and a SELECT of the rows in the "main" table that do not fulfill the join condition. For example,
</p>
<div class="mw-highlight mw-highlight-lang-sql mw-content-ltr" dir="ltr"><pre><span class="k">SELECT</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">LastName</span><span class="p">,</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">DepartmentID</span><span class="p">,</span><span class="w"> </span><span class="n">department</span><span class="p">.</span><span class="n">DepartmentName</span>
<span class="k">FROM</span><span class="w"> </span><span class="n">employee</span>
<span class="k">LEFT</span><span class="w"> </span><span class="k">OUTER</span><span class="w"> </span><span class="k">JOIN</span><span class="w"> </span><span class="n">department</span><span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">DepartmentID</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">department</span><span class="p">.</span><span class="n">DepartmentID</span><span class="p">;</span>
</pre></div>
<p>can also be written as
</p>
<div class="mw-highlight mw-highlight-lang-sql mw-content-ltr" dir="ltr"><pre><span class="k">SELECT</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">LastName</span><span class="p">,</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">DepartmentID</span><span class="p">,</span><span class="w"> </span><span class="n">department</span><span class="p">.</span><span class="n">DepartmentName</span>
<span class="k">FROM</span><span class="w"> </span><span class="n">employee</span>
<span class="k">INNER</span><span class="w"> </span><span class="k">JOIN</span><span class="w"> </span><span class="n">department</span><span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">DepartmentID</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">department</span><span class="p">.</span><span class="n">DepartmentID</span>
<span class="k">UNION</span><span class="w"> </span><span class="k">ALL</span>
<span class="k">SELECT</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">LastName</span><span class="p">,</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">DepartmentID</span><span class="p">,</span><span class="w"> </span><span class="k">cast</span><span class="p">(</span><span class="k">NULL</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nb">varchar</span><span class="p">(</span><span class="mi">20</span><span class="p">))</span>
<span class="k">FROM</span><span class="w"> </span><span class="n">employee</span>
<span class="k">WHERE</span><span class="w"> </span><span class="k">NOT</span><span class="w"> </span><span class="k">EXISTS</span><span class="w"> </span><span class="p">(</span>
<span class="w"> </span><span class="k">SELECT</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">department</span>
<span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="n">employee</span><span class="p">.</span><span class="n">DepartmentID</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">department</span><span class="p">.</span><span class="n">DepartmentID</span><span class="p">)</span>
</pre></div>
<div class="mw-heading mw-heading2"><h2 id="Implementation">Implementation</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1273380762/mw-parser-output/.tmulti">
/* start https://en.wikipedia.org/ */
.mw-parser-output .tmulti .multiimageinner{display:flex;flex-direction:column}.mw-parser-output .tmulti .trow{display:flex;flex-direction:row;clear:left;flex-wrap:wrap;width:100%;box-sizing:border-box}.mw-parser-output .tmulti .tsingle{margin:1px;float:left}.mw-parser-output .tmulti .theader{clear:both;font-weight:bold;text-align:center;align-self:center;background-color:transparent;width:100%}.mw-parser-output .tmulti .thumbcaption{background-color:transparent}.mw-parser-output .tmulti .text-align-left{text-align:left}.mw-parser-output .tmulti .text-align-right{text-align:right}.mw-parser-output .tmulti .text-align-center{text-align:center}@media all and (max-width:720px){.mw-parser-output .tmulti .thumbinner{width:100%!important;box-sizing:border-box;max-width:none!important;align-items:center}.mw-parser-output .tmulti .trow{justify-content:center}.mw-parser-output .tmulti .tsingle{float:none!important;max-width:100%!important;box-sizing:border-box;text-align:center}.mw-parser-output .tmulti .tsingle .thumbcaption{text-align:left}.mw-parser-output .tmulti .trow>.thumbcaption{text-align:center}}@media screen{html.skin-theme-clientpref-night .mw-parser-output .tmulti .multiimageinner span:not(.skin-invert-image):not(.skin-invert):not(.bg-transparent) img{background-color:white}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .tmulti .multiimageinner span:not(.skin-invert-image):not(.skin-invert):not(.bg-transparent) img{background-color:white}}
/* end https://en.wikipedia.org/ */
</style><div class="thumb tmulti tright"><div class="thumbinner multiimageinner" style="width:288px;max-width:288px"><div class="trow"><div class="tsingle" style="width:142px;max-width:142px"><div class="thumbimage"><span typeof="mw:File"></span></div></div><div class="tsingle" style="width:142px;max-width:142px"><div class="thumbimage"><span typeof="mw:File"></span></div></div></div><div class="trow" style="display:flex"><div class="thumbcaption">Two possible <a href="Query_plan" title="Query plan">query plans</a> for the <i><dfn>triangle query</dfn></i> <span class="texhtml">R(A, B) ⋈ S(B, C) ⋈ T(A, C)</span>; the first joins <span class="texhtml mvar" style="font-style:italic;">S</span> and <span class="texhtml mvar" style="font-style:italic;">T</span> first and joins the result with <span class="texhtml mvar" style="font-style:italic;">R</span>, the second joins <span class="texhtml mvar" style="font-style:italic;">R</span> and <span class="texhtml mvar" style="font-style:italic;">S</span> first and joins the result with <span class="texhtml mvar" style="font-style:italic;">T</span></div></div></div></div>
<p>Much work in database-systems has aimed at efficient implementation of joins, because relational systems commonly call for joins, yet face difficulties in optimising their efficient execution. The problem arises because inner joins operate both <a href="Commutative" class="mw-redirect" title="Commutative">commutatively</a> and <a href="Associative" class="mw-redirect" title="Associative">associatively</a>. In practice, this means that the user merely supplies the list of tables for joining and the join conditions to use, and the database system has the task of determining the most efficient way to perform the operation. The choices become more complex as the number of tables involved in a query increases, with each table having different characteristics in record count, average record length (considering NULL fields) and available indexes. Where Clause filters can also significantly impact query volume and cost.
</p><p>A <a href="Query_optimizer" class="mw-redirect" title="Query optimizer">query optimizer</a> determines how to execute a query containing joins. A query optimizer has two basic freedoms:
</p>
<ol><li><b>Join order</b>: Because it joins functions commutatively and associatively, the order in which the system joins tables does not change the final result set of the query. However, join-order <b>could</b> have an enormous impact on the cost of the join operation, so choosing the best join order becomes very important.</li>
<li><b>Join method</b>: Given two tables and a join condition, multiple <a href="Algorithm" title="Algorithm">algorithms</a> can produce the result set of the join. Which algorithm runs most efficiently depends on the sizes of the input tables, the number of rows from each table that match the join condition, and the operations required by the rest of the query.</li></ol>
<p>Many join-algorithms treat their inputs differently. One can refer to the inputs to a join as the "outer" and "inner" join operands, or "left" and "right", respectively. In the case of nested loops, for example, the database system will scan the entire inner relation for each row of the outer relation.
</p><p>One can classify query-plans involving joins as follows:<sup id="cite_ref-Yu1998_12-0" class="reference"><a href="#cite_note-Yu1998-12"><span class="cite-bracket">[</span>12<span class="cite-bracket">]</span></a></sup>
</p>
<dl><dt>left-deep</dt>
<dd>using a base table (rather than another join) as the inner operand of each join in the plan</dd>
<dt>right-deep</dt>
<dd>using a base table as the outer operand of each join in the plan</dd>
<dt>bushy</dt>
<dd>neither left-deep nor right-deep; both inputs to a join may themselves result from joins</dd></dl>
<p>These names derive from the appearance of the <a href="Query_plan" title="Query plan">query plan</a> if drawn as a <a href="Tree_data_structure" class="mw-redirect" title="Tree data structure">tree</a>, with the outer join relation on the left and the inner relation on the right (as convention dictates).
</p>
<div class="mw-heading mw-heading3"><h3 id="Join_algorithms">Join algorithms</h3></div>
<p>Three fundamental algorithms for performing a binary join operation exist: <a href="Nested_loop_join" title="Nested loop join">nested loop join</a>, <a href="Sort-merge_join" title="Sort-merge join">sort-merge join</a> and <a href="Hash_join" title="Hash join">hash join</a>. <a href="Worst-case_optimal_join_algorithm" title="Worst-case optimal join algorithm">Worst-case optimal join algorithms</a> are asymptotically faster than binary join algorithms for joins between more than two relations in the <a href="Worst_case" class="mw-redirect" title="Worst case">worst case</a>.
</p>
<div class="mw-heading mw-heading3"><h3 id="Join_indexes">Join indexes</h3></div>
<p>Join indexes are <a href="Database_index" title="Database index">database indexes</a> that facilitate the processing of join queries in <a href="Data_warehouse" title="Data warehouse">data warehouses</a>: they are currently (2012) available in implementations by <a href="Oracle_database" class="mw-redirect" title="Oracle database">Oracle</a><sup id="cite_ref-14" class="reference"><a href="#cite_note-14"><span class="cite-bracket">[</span>14<span class="cite-bracket">]</span></a></sup> and <a href="Teradata" title="Teradata">Teradata</a>.<sup id="cite_ref-15" class="reference"><a href="#cite_note-15"><span class="cite-bracket">[</span>15<span class="cite-bracket">]</span></a></sup>
</p><p>In the Teradata implementation, specified columns, aggregate functions on columns, or components of date columns from one or more tables are specified using a syntax similar to the definition of a <a href="Database_view" class="mw-redirect" title="Database view">database view</a>: up to 64 columns/column expressions can be specified in a single join index. Optionally, a column that defines the <a href="Primary_key" title="Primary key">primary key</a> of the composite data may also be specified: on parallel hardware, the column values are used to partition the index's contents across multiple disks. When the source tables are updated interactively by users, the contents of the join index are automatically updated. Any query whose WHERE clause specifies any combination of columns or column expressions that are an exact subset of those defined in a join index (a so-called "covering query") will cause the join index, rather than the original tables and their indexes, to be consulted during query execution.
</p><p>The Oracle implementation limits itself to using <a href="Bitmap_index" title="Bitmap index">bitmap indexes</a>. A <i>bitmap join index</i> is used for low-cardinality columns (i.e., columns containing fewer than 300 distinct values, according to the Oracle documentation): it combines low-cardinality columns from multiple related tables. The example Oracle uses is that of an inventory system, where different suppliers provide different parts. The <a href="Database_schema" title="Database schema">schema</a> has three linked tables: two "master tables", Part and Supplier, and a "detail table", Inventory. The last is a many-to-many table linking Supplier to Part, and contains the most rows. Every part has a Part Type, and every supplier is based in the US, and has a State column. There are not more than 60 states+territories in the US, and not more than 300 Part Types. The bitmap join index is defined using a standard three-table join on the three tables above, and specifying the Part_Type and Supplier_State columns for the index. However, it is defined on the Inventory table, even though the columns Part_Type and Supplier_State are "borrowed" from Supplier and Part respectively.
</p><p>As for Teradata, an Oracle bitmap join index is only utilized to answer a query when the query's WHERE clause specifies columns limited to those that are included in the join index.
</p>
<div class="mw-heading mw-heading3"><h3 id="Straight_join">Straight join</h3></div>
<p>Some database systems allow the user to force the system to read the tables in a join in a particular order. This is used when the join optimizer chooses to read the tables in an inefficient order. For example, in <a href="MySQL" title="MySQL">MySQL</a> the command <code>STRAIGHT_JOIN</code> reads the tables in exactly the order listed in the query.<sup id="cite_ref-16" class="reference"><a href="#cite_note-16"><span class="cite-bracket">[</span>16<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2></div>
<ul><li><a href="Join_(relational_algebra)" title="Join (relational algebra)">Join (relational algebra)</a></li>
<li><a href="Antijoin" class="mw-redirect" title="Antijoin">Antijoin</a></li>
<li><a href="Set_operations_(SQL)" title="Set operations (SQL)">Set operations (SQL)</a></li></ul>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<div class="mw-heading mw-heading3"><h3 id="Citations">Citations</h3></div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */
.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}
/* end https://en.wikipedia.org/ */
</style><div class="reflist">
<div class="mw-references-wrap mw-references-columns"><ol class="references">
<li id="cite_note-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-1">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://www.sqlguides.com/sql_cross_join.php">SQL CROSS JOIN</a></span>
</li>
<li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text">Greg Robidoux, "Avoid SQL Server functions in the WHERE clause for Performance", MSSQL Tips, 3 May 2007</span>
</li>
<li id="cite_note-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-3">^</a></b></span> <span class="reference-text">Patrick Wolf, "Inside Oracle APEX "Caution when using PL/SQL functions in a SQL statement", 30 November 2006</span>
</li>
<li id="cite_note-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-4">^</a></b></span> <span class="reference-text">Gregory A. Larsen, "T-SQL Best Practices - Don't Use Scalar Value Functions in Column List or WHERE Clauses", 29 October 2009,</span>
</li>
<li id="cite_note-5"><span class="mw-cite-backlink"><b><a href="#cite_ref-5">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://www.java2s.com/Tutorial/Oracle/0140__Table-Joins/SimplifyingJoinswiththeUSINGKeyword.htm">Simplifying Joins with the USING Keyword</a></span>
</li>
<li id="cite_note-6"><span class="mw-cite-backlink"><b><a href="#cite_ref-6">^</a></b></span> <span class="reference-text">In <a href="Unicode" title="Unicode">Unicode</a>, the bowtie symbol is ⋈ (U+22C8).</span>
</li>
<li id="cite_note-7"><span class="mw-cite-backlink"><b><a href="#cite_ref-7">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:13430766143199">Ask Tom "Oracle support of ANSI joins."</a> <a rel="nofollow" class="external text" href="http://awads.net/wp/2006/03/20/back-to-basics-inner-joins/#comment-2837">Back to basics: inner joins » Eddie Awad's Blog</a> <a rel="nofollow" class="external text" href="https://web.archive.org/web/20101119182541/http://awads.net/wp/2006/03/20/back-to-basics-inner-joins/#comment-2837">Archived</a> 2010-11-19 at the <a href="Wayback_Machine" title="Wayback Machine">Wayback Machine</a></span>
</li>
<li id="cite_note-8"><span class="mw-cite-backlink"><b><a href="#cite_ref-8">^</a></b></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */
.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}
/* end https://en.wikipedia.org/ */
</style><cite id="CITEREFSilberschatzKorthSudarshan2002" class="citation book cs1"><a href="Abraham_Silberschatz" title="Abraham Silberschatz">Silberschatz, Abraham</a>; <a href="Henry_F._Korth" title="Henry F. Korth">Korth, Hank</a>; Sudarshan, S. (2002). "Section 4.10.2: Join Types and Conditions". <i>Database System Concepts</i> (4th ed.). McGraw-Hill. p. 166. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>0072283637</bdi>.</cite></span>
</li>
<li id="cite_note-deprecated_plus_sign-9"><span class="mw-cite-backlink"><b><a href="#cite_ref-deprecated_plus_sign_9-0">^</a></b></span> <span class="reference-text">
<a rel="nofollow" class="external text" href="http://www.dba-oracle.com/tips_oracle_left_outer_join.htm">Oracle Left Outer Join</a></span>
</li>
<li id="cite_note-10"><span class="mw-cite-backlink"><b><a href="#cite_ref-10">^</a></b></span> <span class="reference-text"><a href="#CITEREFShah2005">Shah 2005</a>, p. 165</span>
</li>
<li id="cite_note-11"><span class="mw-cite-backlink"><b><a href="#cite_ref-11">^</a></b></span> <span class="reference-text">Adapted from <a href="#CITEREFPratt2005">Pratt 2005</a>, pp. 115–6</span>
</li>
<li id="cite_note-Yu1998-12"><span class="mw-cite-backlink"><b><a href="#cite_ref-Yu1998_12-0">^</a></b></span> <span class="reference-text"><a href="#CITEREFYuMeng1998">Yu & Meng 1998</a>, p. 213</span>
</li>
<li id="cite_note-13"><span class="mw-cite-backlink"><b><a href="#cite_ref-13">^</a></b></span> <span class="reference-text"><cite id="CITEREFWangWillseySuciu2023" class="citation arxiv cs1">Wang, Yisu Remy; Willsey, Max; Suciu, Dan (2023-01-27). "Free Join: Unifying Worst-Case Optimal and Traditional Joins". <a href="ArXiv_(identifier)" class="mw-redirect" title="ArXiv (identifier)">arXiv</a>:<span class="id-lock-free" title="Freely accessible"><a rel="nofollow" class="external text" href="https://arxiv.org/abs/2301.10841">2301.10841</a></span> [<a rel="nofollow" class="external text" href="https://arxiv.org/archive/cs.DB">cs.DB</a>].</cite></span>
</li>
<li id="cite_note-14"><span class="mw-cite-backlink"><b><a href="#cite_ref-14">^</a></b></span> <span class="reference-text">Oracle Bitmap Join Indexes.
<cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://docs.oracle.com/en/database/oracle/oracle-database/23/cncpt/indexes-and-index-organized-tables.html#GUID-3286EBA4-0D5B-423D-815B-997A3E4B4B6C">"Database Concepts - 5 Indexes and Index-Organized Tables - Bitmap Join Indexes"</a><span class="reference-accessdate">. Retrieved <span class="nowrap">2024-06-23</span></span>.</cite></span>
</li>
<li id="cite_note-15"><span class="mw-cite-backlink"><b><a href="#cite_ref-15">^</a></b></span> <span class="reference-text">Teradata Join Indexes.
<cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://docs.teradata.com/r/Enterprise_IntelliFlex_VMware/SQL-Data-Definition-Language-Syntax-and-Examples/Index-Statements/CREATE-JOIN-INDEX">"SQL Data Definition Language Syntax and Examples - CREATE JOIN INDEX"</a><span class="reference-accessdate">. Retrieved <span class="nowrap">2024-06-23</span></span>.</cite></span>
</li>
<li id="cite_note-16"><span class="mw-cite-backlink"><b><a href="#cite_ref-16">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://dev.mysql.com/doc/refman/5.7/en/join.html">"13.2.9.2 JOIN Syntax"</a>. <i>MySQL 5.7 Reference Manual</i>. <a href="Oracle_Corporation" title="Oracle Corporation">Oracle Corporation</a><span class="reference-accessdate">. Retrieved <span class="nowrap">2015-12-03</span></span>.</cite></span>
</li>
</ol></div></div>
<div class="mw-heading mw-heading3"><h3 id="Sources">Sources</h3></div>
<style data-mw-deduplicate="TemplateStyles:r1239549316">
/* start https://en.wikipedia.org/ */
.mw-parser-output .refbegin{margin-bottom:0.5em}.mw-parser-output .refbegin-hanging-indents>ul{margin-left:0}.mw-parser-output .refbegin-hanging-indents>ul>li{margin-left:0;padding-left:3.2em;text-indent:-3.2em}.mw-parser-output .refbegin-hanging-indents ul,.mw-parser-output .refbegin-hanging-indents ul li{list-style:none}@media(max-width:720px){.mw-parser-output .refbegin-hanging-indents>ul>li{padding-left:1.6em;text-indent:-1.6em}}.mw-parser-output .refbegin-columns{margin-top:0.3em}.mw-parser-output .refbegin-columns ul{margin-top:0}.mw-parser-output .refbegin-columns li{page-break-inside:avoid;break-inside:avoid-column}@media screen{.mw-parser-output .refbegin{font-size:90%}}
/* end https://en.wikipedia.org/ */
</style><div class="refbegin" style="">
<ul><li><cite id="CITEREFPratt2005" class="citation cs2">Pratt, Phillip J (2005), <span class="id-lock-registration" title="Free registration required"><a rel="nofollow" class="external text" href="https://archive.org/details/guidetosql00prat"><i>A Guide To SQL, Seventh Edition</i></a></span>, Thomson Course Technology, <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-0-619-21674-0</bdi></cite></li>
<li><cite id="CITEREFShah2005" class="citation cs2">Shah, Nilesh (2005) [2002], <i>Database Systems Using Oracle – A Simplified Guide to SQL and PL/SQL Second Edition</i> (International ed.), Pearson Education International, <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>0-13-191180-5</bdi></cite></li>
<li><cite id="CITEREFYuMeng1998" class="citation cs2">Yu, Clement T.; Meng, Weiyi (1998), <a rel="nofollow" class="external text" href="https://books.google.com/books?id=aBHRDhrrehYC"><i>Principles of Database Query Processing for Advanced Applications</i></a>, Morgan Kaufmann, <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-1-55860-434-6</bdi><span class="reference-accessdate">, retrieved <span class="nowrap">2009-03-03</span></span></cite></li></ul>
</div>
<div class="mw-heading mw-heading2"><h2 id="External_links">External links</h2></div>
<ul><li>Specific to products:
<ul><li><a rel="nofollow" class="external text" href="http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.ase_15.0.sqlug/html/sqlug/sqlug138.htm">Sybase ASE 15 Joins</a></li>
<li><a rel="nofollow" class="external text" href="http://dev.mysql.com/doc/refman/8.0/en/join.html">MySQL 8.0 Joins</a></li>
<li><a rel="nofollow" class="external text" href="https://www.postgresql.org/docs/14/tutorial-join.html">PostgreSQL 14 Joins</a></li>
<li><a rel="nofollow" class="external text" href="https://docs.microsoft.com/en-us/sql/relational-databases/performance/joins?view=sql-server-ver15">Joins in Microsoft SQL Server</a></li>
<li><a rel="nofollow" class="external text" href="http://maxdb.sap.com/currentdoc/45/f31c38e95511d5995d00508b5d5211/content.htm">Joins in MaxDB 7.6</a></li>
<li><a rel="nofollow" class="external text" href="http://docs.oracle.com/cd/E16655_01/server.121/e17209/queries006.htm">Joins in Oracle 12c R1</a></li>
<li><a rel="nofollow" class="external text" href="https://oracletutorial.net/oracle-sql-joins.html">Oracle SQL Joins</a></li></ul></li></ul>
<div style="clear:both;" class=""></div>
<div class="navbox-styles"><style data-mw-deduplicate="TemplateStyles:r1129693374">
/* start https://en.wikipedia.org/ */
.mw-parser-output .hlist dl,.mw-parser-output .hlist ol,.mw-parser-output .hlist ul{margin:0;padding:0}.mw-parser-output .hlist dd,.mw-parser-output .hlist dt,.mw-parser-output .hlist li{margin:0;display:inline}.mw-parser-output .hlist.inline,.mw-parser-output .hlist.inline dl,.mw-parser-output .hlist.inline ol,.mw-parser-output .hlist.inline ul,.mw-parser-output .hlist dl dl,.mw-parser-output .hlist dl ol,.mw-parser-output .hlist dl ul,.mw-parser-output .hlist ol dl,.mw-parser-output .hlist ol ol,.mw-parser-output .hlist ol ul,.mw-parser-output .hlist ul dl,.mw-parser-output .hlist ul ol,.mw-parser-output .hlist ul ul{display:inline}.mw-parser-output .hlist .mw-empty-li{display:none}.mw-parser-output .hlist dt::after{content:": "}.mw-parser-output .hlist dd::after,.mw-parser-output .hlist li::after{content:" · ";font-weight:bold}.mw-parser-output .hlist dd:last-child::after,.mw-parser-output .hlist dt:last-child::after,.mw-parser-output .hlist li:last-child::after{content:none}.mw-parser-output .hlist dd dd:first-child::before,.mw-parser-output .hlist dd dt:first-child::before,.mw-parser-output .hlist dd li:first-child::before,.mw-parser-output .hlist dt dd:first-child::before,.mw-parser-output .hlist dt dt:first-child::before,.mw-parser-output .hlist dt li:first-child::before,.mw-parser-output .hlist li dd:first-child::before,.mw-parser-output .hlist li dt:first-child::before,.mw-parser-output .hlist li li:first-child::before{content:" (";font-weight:normal}.mw-parser-output .hlist dd dd:last-child::after,.mw-parser-output .hlist dd dt:last-child::after,.mw-parser-output .hlist dd li:last-child::after,.mw-parser-output .hlist dt dd:last-child::after,.mw-parser-output .hlist dt dt:last-child::after,.mw-parser-output .hlist dt li:last-child::after,.mw-parser-output .hlist li dd:last-child::after,.mw-parser-output .hlist li dt:last-child::after,.mw-parser-output .hlist li li:last-child::after{content:")";font-weight:normal}.mw-parser-output .hlist ol{counter-reset:listitem}.mw-parser-output .hlist ol>li{counter-increment:listitem}.mw-parser-output .hlist ol>li::before{content:" "counter(listitem)"\a0 "}.mw-parser-output .hlist dd ol>li:first-child::before,.mw-parser-output .hlist dt ol>li:first-child::before,.mw-parser-output .hlist li ol>li:first-child::before{content:" ("counter(listitem)"\a0 "}
/* end https://en.wikipedia.org/ */
</style><style data-mw-deduplicate="TemplateStyles:r1236075235">
/* start https://en.wikipedia.org/ */
.mw-parser-output .navbox{box-sizing:border-box;border:1px solid #a2a9b1;width:100%;clear:both;font-size:88%;text-align:center;padding:1px;margin:1em auto 0}.mw-parser-output .navbox .navbox{margin-top:0}.mw-parser-output .navbox+.navbox,.mw-parser-output .navbox+.navbox-styles+.navbox{margin-top:-1px}.mw-parser-output .navbox-inner,.mw-parser-output .navbox-subgroup{width:100%}.mw-parser-output .navbox-group,.mw-parser-output .navbox-title,.mw-parser-output .navbox-abovebelow{padding:0.25em 1em;line-height:1.5em;text-align:center}.mw-parser-output .navbox-group{white-space:nowrap;text-align:right}.mw-parser-output .navbox,.mw-parser-output .navbox-subgroup{background-color:#fdfdfd}.mw-parser-output .navbox-list{line-height:1.5em;border-color:#fdfdfd}.mw-parser-output .navbox-list-with-group{text-align:left;border-left-width:2px;border-left-style:solid}.mw-parser-output tr+tr>.navbox-abovebelow,.mw-parser-output tr+tr>.navbox-group,.mw-parser-output tr+tr>.navbox-image,.mw-parser-output tr+tr>.navbox-list{border-top:2px solid #fdfdfd}.mw-parser-output .navbox-title{background-color:#ccf}.mw-parser-output .navbox-abovebelow,.mw-parser-output .navbox-group,.mw-parser-output .navbox-subgroup .navbox-title{background-color:#ddf}.mw-parser-output .navbox-subgroup .navbox-group,.mw-parser-output .navbox-subgroup .navbox-abovebelow{background-color:#e6e6ff}.mw-parser-output .navbox-even{background-color:#f7f7f7}.mw-parser-output .navbox-odd{background-color:transparent}.mw-parser-output .navbox .hlist td dl,.mw-parser-output .navbox .hlist td ol,.mw-parser-output .navbox .hlist td ul,.mw-parser-output .navbox td.hlist dl,.mw-parser-output .navbox td.hlist ol,.mw-parser-output .navbox td.hlist ul{padding:0.125em 0}.mw-parser-output .navbox .navbar{display:block;font-size:100%}.mw-parser-output .navbox-title .navbar{float:left;text-align:left;margin-right:0.5em}body.skin--responsive .mw-parser-output .navbox-image img{max-width:none!important}@media print{body.ns-0 .mw-parser-output .navbox{display:none!important}}
/* end https://en.wikipedia.org/ */
</style><style data-mw-deduplicate="TemplateStyles:r920966791">
/* start https://en.wikipedia.org/ */
.mw-parser-output span.smallcaps{font-variant:small-caps}.mw-parser-output span.smallcaps-smaller{font-size:85%}
/* end https://en.wikipedia.org/ */
</style></div><div role="navigation" class="navbox" aria-labelledby="SQL70" style="padding:3px"><table class="nowraplinks mw-collapsible autocollapse navbox-inner" style="border-spacing:0;background:transparent;color:inherit"><tbody><tr><th scope="col" class="navbox-title" colspan="2"><style data-mw-deduplicate="TemplateStyles:r1239400231">
/* start https://en.wikipedia.org/ */
.mw-parser-output .navbar{display:inline;font-size:88%;font-weight:normal}.mw-parser-output .navbar-collapse{float:left;text-align:left}.mw-parser-output .navbar-boxtext{word-spacing:0}.mw-parser-output .navbar ul{display:inline-block;white-space:nowrap;line-height:inherit}.mw-parser-output .navbar-brackets::before{margin-right:-0.125em;content:"[ "}.mw-parser-output .navbar-brackets::after{margin-left:-0.125em;content:" ]"}.mw-parser-output .navbar li{word-spacing:-0.125em}.mw-parser-output .navbar a>span,.mw-parser-output .navbar a>abbr{text-decoration:inherit}.mw-parser-output .navbar-mini abbr{font-variant:small-caps;border-bottom:none;text-decoration:none;cursor:inherit}.mw-parser-output .navbar-ct-full{font-size:114%;margin:0 7em}.mw-parser-output .navbar-ct-mini{font-size:114%;margin:0 4em}html.skin-theme-clientpref-night .mw-parser-output .navbar li a abbr{color:var(--color-base)!important}@media(prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .navbar li a abbr{color:var(--color-base)!important}}@media print{.mw-parser-output .navbar{display:none!important}}
/* end https://en.wikipedia.org/ */
</style><div id="SQL70" style="font-size:114%;margin:0 4em"><a href="SQL" title="SQL">SQL</a></div></th></tr><tr><th scope="row" class="navbox-group" style="width:1%">Versions</th><td class="navbox-list-with-group navbox-list navbox-odd hlist" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="SEQUEL" class="mw-redirect" title="SEQUEL">SEQUEL</a></li>
<li>SQL-86</li>
<li>SQL-89</li>
<li><a href="SQL-92" title="SQL-92">SQL-92</a></li>
<li><a href="SQL%3A1999" title="SQL:1999">SQL:1999</a></li>
<li><a href="SQL%3A2003" title="SQL:2003">SQL:2003</a></li>
<li><a href="SQL%3A2006" title="SQL:2006">SQL:2006</a></li>
<li><a href="SQL%3A2008" title="SQL:2008">SQL:2008</a></li>
<li><a href="SQL%3A2011" title="SQL:2011">SQL:2011</a></li>
<li><a href="SQL%3A2016" title="SQL:2016">SQL:2016</a></li>
<li><a href="SQL%3A2023" title="SQL:2023">SQL:2023</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="SQL_reserved_words" class="mw-redirect" title="SQL reserved words">Keywords</a></th><td class="navbox-list-with-group navbox-list navbox-even hlist" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><span class="smallcaps"><a href="Alias_(SQL)" title="Alias (SQL)">As</a></span></li>
<li><span class="smallcaps"><a href="Case_(SQL)" class="mw-redirect" title="Case (SQL)">Case</a></span></li>
<li><span class="smallcaps"><a href="Create_(SQL)" class="mw-redirect" title="Create (SQL)">Create</a></span></li>
<li><span class="smallcaps"><a href="Delete_(SQL)" title="Delete (SQL)">Delete</a></span></li>
<li><span class="smallcaps"><a href="From_(SQL)" title="From (SQL)">From</a></span></li>
<li><span class="smallcaps"><a href="Group_by_(SQL)" title="Group by (SQL)">Group by</a></span></li>
<li><span class="smallcaps"><a href="Having_(SQL)" title="Having (SQL)">Having</a></span></li>
<li><span class="smallcaps"><a href="Insert_(SQL)" title="Insert (SQL)">Insert</a></span></li>
<li></li>
<li><span class="smallcaps"><a href="Merge_(SQL)" title="Merge (SQL)">Merge</a></span></li>
<li><span class="smallcaps"><a href="Null_(SQL)" title="Null (SQL)">Null</a></span></li>
<li><span class="smallcaps"><a href="Order_by" title="Order by">Order by</a></span></li>
<li><span class="smallcaps"><a href="Window_function_(SQL)" title="Window function (SQL)">Over</a></span></li>
<li><span class="smallcaps"><a href="Prepare_(SQL)" class="mw-redirect" title="Prepare (SQL)">Prepare</a></span></li>
<li><span class="smallcaps"><a href="Select_(SQL)" title="Select (SQL)">Select</a></span></li>
<li><span class="smallcaps"><a href="Truncate_(SQL)" title="Truncate (SQL)">Truncate</a></span></li>
<li><span class="smallcaps"><a href="Set_operations_(SQL)" title="Set operations (SQL)">Union</a></span></li>
<li><span class="smallcaps"><a href="Update_(SQL)" title="Update (SQL)">Update</a></span></li>
<li><span class="smallcaps"><a href="With_(SQL)" class="mw-redirect" title="With (SQL)">With</a></span></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%">Related</th><td class="navbox-list-with-group navbox-list navbox-odd hlist" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Edgar_F._Codd" title="Edgar F. Codd">Edgar Codd</a></li>
<li><a href="Relational_database" title="Relational database">Relational database</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%">ISO/IEC SQL parts</th><td class="navbox-list-with-group navbox-list navbox-even hlist" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li>Framework</li>
<li>Foundation</li>
<li><a href="SQL/CLI" class="mw-redirect" title="SQL/CLI">Call-Level Interface</a></li>
<li><a href="SQL/PSM" title="SQL/PSM">Persistent Stored Modules</a></li>
<li><a href="SQL/MED" title="SQL/MED">Management of External Data</a></li>
<li><a href="SQL/OLB" title="SQL/OLB">Object Language Bindings</a></li>
<li><a href="SQL/Schemata" title="SQL/Schemata">Information and Definition Schemas</a></li>
<li><a href="SQL/JRT" title="SQL/JRT">SQL Routines and Types for the Java Programming Language</a></li>
<li><a href="SQL/XML" title="SQL/XML">XML-Related Specifications</a></li></ul>
</div></td></tr></tbody></table></div></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-07-10" href="https://en.wikipedia.org/wiki/?title=Join_(SQL)&oldid=1299838577">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>
</body></html>